﻿/**************************************************
*菜单加载模块
 * 数据访问层  
 * 内涵一个递归方法
 * Author：JXL
 
 *************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using IDAL;
using Model;
using System.Data;
using System.Data.SqlClient;


namespace DAL
{
    public class AuthorityDAL : BaseDAL, IAuthority
    {

        protected override string tablename
        {
            get { return "Authority"; }
        }

        protected override Dictionary<string, bool> pkname
        {
            get
            {
                var id = new Dictionary<string, bool>();
                id.Add("authority_Id", true);
                return id;
            }
        }
        /// <summary>
        /// 获取全部的功能菜单
        /// </summary>
        /// <returns></returns>
        public List<AuthorList<Authority>> GetAuthorList(int id)
        {
            string str = "select a.* from Authority a,Roles b,AuthorityAndRoles c where b.Roles_Id=c.Roles_Id and c.AuthorID=a.Authority_Id and b.Roles_Id="+id+" ";
            using(SqlConnection sql=new SqlConnection (HouseCommon.Common.conn))
            {
                sql.Open();
                SqlCommand cmd = new SqlCommand(str, sql);
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataTable ta = new DataTable();
                da.Fill(ta);
                var list = DBhelper.DBHelper.DataTable2List<Authority>(ta).ToList();
                var ll = lst(list.Where(n => n.parentId == 0).ToList(), list);
                return ll;
            }
            
            
            
        }
        /// <summary>
        /// 循环菜单的加载成层级结构
        /// </summary>
        /// <param name="list"></param>
        /// <param name="lis"></param>
        /// <returns></returns>
        public List<AuthorList<Authority>> lst(List<Authority> list,List<Authority> lis)
        {
            List<AuthorList<Authority>> lists = new List<AuthorList<Authority>>();
            foreach (var i in list)
            {
                
                AuthorList<Authority> al = new AuthorList<Authority>();
                al.authority_Name = i.authority_Name;
                al.authority_Url = i.authority_Url;
                al.child_menus = lst(lis.Where(n => n.parentId.Equals(i.authority_Id)).ToList(),lis);
                lists.Add(al);
            }
            var ll = lists;
            return ll;


        }
    }
}
